<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Doctors_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'desc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}	
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'doctors-all_doctors', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			
			if($response->message !== 'No Doctors'){
			$data['doctors_all_category'] = $response;
			
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/doctors_ctrl/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['doctors_all_category'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['doctors_fileds'] = array('doctor_name'=>"Doctor's Name", 'category_name'=>'Category','created_date'=>'Date');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$this->load->view('admin/doctors_view', $data);
 		}
		
		function search_by(){
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search = $this->input->post('search')
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/doctors_ctrl/index/'.$query_id);
			
		}	
		
		/* function create_doctors_page(){
			$this->load->model('admin_doctors_model');
			$data = array('www-command'=>'doctors-get_doctorcategory_list');
			$get_doctorcategory_list = $this->admin_doctors_model->doctors_list($data);
			$data = array('www-command'=>'doctors-get_doctorstates_list');
			$get_doctorstates_list = $this->admin_doctors_model->doctors_list($data);
			$data['get_doctorcategory_list'] = $get_doctorcategory_list;
			$data['get_doctorstates_list'] = $get_doctorstates_list;
			//echo '<pre>';print_r($get_doctorstates_list);exit;
			
			$this->load->view('admin/create_doctors_page', $data);
		} */
		
		function create_doctors(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('category', '', 'callback_category');
				$this->form_validation->set_rules('address', 'Address', 'required');
				$this->form_validation->set_rules('city', 'City', 'required|alpha');
				$this->form_validation->set_rules('state', 'State', 'callback_state');
				$this->form_validation->set_rules('zip', 'Zip', 'required|numeric|max_length[6]');
				$this->form_validation->set_rules('comments', 'Comments', 'max_length[500]');
				
				if($this->form_validation->run() == FALSE){
					
			$data = array('www-command'=>'doctors-get_doctorcategory_list');
			
			$url = CURL_URL;
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$get_doctorcategory_list = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			
			$data = array('www-command'=>'doctors-get_doctorstates_list');
			
			$url = CURL_URL;
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$get_doctorstates_list = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			
			$data['get_doctorcategory_list'] = $get_doctorcategory_list;
			$data['get_doctorstates_list'] = $get_doctorstates_list;
			//echo '<pre>';print_r($get_doctorstates_list);exit;
			
			$this->load->view('admin/create_doctors_page', $data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'doctors-create_doctor', 
					'doctor_name' => $this->input->post('name'),
					'category_id' => $this->input->post('category'),
					'doctor_address' => $this->input->post('address'),
					'doctor_city' => $this->input->post('city'),
					'state_id' => $this->input->post('state'),
					'doctor_zipcode' => $this->input->post('zip'),
					'doctor_comments' => $this->input->post('comments')
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/doctors_ctrl/index/');
				}
		}
		
		function state($state){
			if($state == ""){
				$this->form_validation->set_message('state','State field is required');
				return false;
			}else{
				return true;
			}
		}
		
		function category($category){
			if($category == ""){
				$this->form_validation->set_message('category','Category field is required');
				return false;
			}else{
				return true;
			}
		}
		
		/* function edit_doctors_page(){
			$this->load->model('admin_doctors_model');
			$data = array('www-command'=>'doctors-get_doctorcategory_list');
			$get_doctorcategory_list = $this->admin_doctors_model->doctors_list($data);
			$data = array('www-command'=>'doctors-get_doctorstates_list');
			$get_doctorstates_list = $this->admin_doctors_model->doctors_list($data);
			$data = array('www-command'=>'doctors-get_doctor', 'doctor_id'=>$this->uri->segment(4));
			$get_doctor = $this->admin_doctors_model->doctors_list($data);
			$data['get_doctorcategory_list'] = $get_doctorcategory_list;
			$data['get_doctorstates_list'] = $get_doctorstates_list;
			$data['get_doctor'] = $get_doctor;
			//echo '<pre>';print_r($get_doctor);exit;
			$this->load->view('admin/edit_doctors_page', $data);
		} */
		
		function edit_doctors(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('category', '', 'callback_category');
				$this->form_validation->set_rules('address', 'Address', 'required');
				$this->form_validation->set_rules('city', 'City', 'required|alpha');
				$this->form_validation->set_rules('state', 'State', 'callback_state');
				$this->form_validation->set_rules('zip', 'Zip', 'required|numeric|max_length[6]');
				$this->form_validation->set_rules('comments', 'Comments', 'max_length[500]');
				
				if($this->form_validation->run() == FALSE){
					$data = array('www-command'=>'doctors-get_doctorcategory_list');
			
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_doctorcategory_list = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($get_doctorcategory_list);exit;
				curl_close($ch);
			
				$data = array('www-command'=>'doctors-get_doctorstates_list');
				
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_doctorstates_list = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($get_doctorstates_list);exit;
				curl_close($ch);
				
				if($this->uri->segment(4) > 0){
					$_SESSION['doctorid'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'doctors-get_doctor', 'doctor_id'=>$_SESSION['doctorid']);
				
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_doctor = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($get_doctor);exit;
				curl_close($ch);
				
				$data['get_doctor'] = $get_doctor;
				$data['get_doctorcategory_list'] = $get_doctorcategory_list;
				$data['get_doctorstates_list'] = $get_doctorstates_list;
					
						$this->load->view('admin/edit_doctors_page',$data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'doctors-edit_doctor', 
					'doctor_name' => $this->input->post('name'),
					'category_id' => $this->input->post('category'),
					'doctor_address' => $this->input->post('address'),
					'doctor_city' => $this->input->post('city'),
					'state_id' => $this->input->post('state'),
					'doctor_zipcode' => $this->input->post('zip'),
					'doctor_comments' => $this->input->post('comments'),
					'doctor_id' => $_SESSION['doctorid']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/doctors_ctrl/index/');
				}
		}
		
		function delete_doctors(){
			$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'doctors-delete_doctor','status'=>$status, 'doctor_id'=>$this->uri->segment(4));
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/doctors_ctrl/index/');
		}
	}
